<?php

/**
 *   Expectation for the DB Result Set Assertion
 */

class Expectation_DB extends Expectation{
    /**
     *    the number of the first different rows
     */
    private $indexdiff = -1;

    /**
     *    the first row which different with the value
     */
    private $comparediff = null;

    /**
     *    Set the value which compare against
     *    @param  value Array              Test Value to match
     *    @param  message string           Customized message format
     *    @access public
     */
    function __construct($value, $message = '%s'){
        parent::__construct($message);
        $this->value = $value;
    }

    /**
     *    Tests the expectation. True if it matches the
     *    held value.
     *    @param mixed $compare        Comparison value.
     *    @return boolean              True if correct.
     *    @access public
     */
    function test($compare){
        if(!is_array($compare) || !is_array($this->value)){
            return false;
        }
        else{
            $count = count($compare);
            for($i = 0;$i<$count;$i++){
                if(count(array_diff($this->value[$i],$compare[$i])) !== 0 ||
                   count(array_diff($compare[$i],$this->value[$i])) !== 0){
                    $this->comparediff = $compare[$i];
                    $this->indexdiff = $i;
                    return false;
                }
            }
        }
        $this->compareResult = true;
        return true;
    }

    /**
     *    Returns a human readable test message.
     *    @param mixed $compare      Comparison value.
     *    @return string             Description of success
     *                               or failure.
     *    @access public
     */
    function testMessage($compare){
        if($this->compareResult){
            return  "Equal expectation success";
        }
        else{
            $line = $this->indexdiff+1;
            return  "Equal expectation fail [ line {$line} ] \n act is : ".implode(",",$this->value[$this->indexdiff]). "\n exp is : ".implode(",",$this->comparediff)."\n";
        }
    }

    /**
     *    Accessor for comparison value.
     *    @return mixed       Held value to compare with.
     *    @access protected
     */
    protected function getValue() {
        return $this->value;
    }

    /**
     *    Unset the DBResult to release the 
     *    memory
     */
    function __destruct(){
        unset($this->value);
    }
}

?>
